<template>
  <div class="monitor-detail-forecast">
  	<div class="monitor-forecast-title detail-title">
		客观预报
	</div>
	<div class="monitor-forecast-content detail-content">
		<div v-for="(item, index) in forecast">
			<div class="content-title detail-content-title">
				{{ item.title }}
			</div>
			<div class="content-lab detail-content-lab">
				<div  v-for="(items, indexs) in item.content" :title="items.name" @click="activeForecast(items)" :class="{active: items.active}">
					{{ items.name }}
				</div>
			</div>
		</div>
	</div>
	<div class="monitor-forecast-close detail-close" @click="closeForecast">
		×
	</div>
	<common ref="common"></common>
  </div>
</template>

<script>
import common from '../common'
import {getMonitorForecast, HTTPY, HTTPG} from '../../mock/mockdata.js'
export default {
	props: {
		initTime: {
			type: String,
		}
	},
	data() {
		return {
			forecast: [],
			storeForecast: {},
			num: 0
		}
	},
	components: {
		common
	},
	created() {
		this.forecast = getMonitorForecast
		this.forecast.forEach((obj) => {
			obj.content.forEach((data) => {
				if(data.active){
					this.storeForecast = data
				}
			})
		})
	},
	watch: {
		initTime: {
			handler: function(val, oldval) {
				this.getLastTime()
			}
		}
	},
	methods: {
		closeForecast() {
			this.$emit('close-forecast')
		},
		activeForecast(res) {
			this.forecast.forEach((obj) => {
				obj.content.forEach((data) =>{
					data.active = false
				})
			})
			res.active = true

			this.storeForecast = res

			this.getLastTime()
		},
		getLastTime() {
			//console.log(this.storeForecast)
			if(this.storeForecast){
				let initTime
				if(!this.storeForecast.beiJing){
					// 暂定固定时间，后期添加日历选择框，固定为08或20时
					initTime = this.$refs.common.getthisTime().slice(0,8)+"080000"
					initTime = this.$refs.common.getTimeCShow(initTime, -8*60*this.num)
				}else{
					initTime = this.$refs.common.getthisTime().slice(0,8)+"080000"
				}
				let urls = HTTPY.url + this.storeForecast.nav+'/getContourEleValidtimeListByTime?userid='+HTTPY.userid+'&pwd=123&productcategory='+this.storeForecast.type+'&fcstlevel='+this.storeForecast.fcstlevel+'&time='+initTime+'&timetype='+this.storeForecast.timetype+'&admincodes=330000&elements=time,hours&limitcnt=100&dataformat=json';
				this.$http.get(urls)
				.then((res) => {
					//console.log(res)
					if(res.body.returnCode === -1){
						if(this.num < 30){
							this.num ++
							this.getLastTime()
						}
					}else{
						this.num = 0
						this.$emit("forecastChange", res.body.DS, this.storeForecast)
					}
				}, (err) => {

				})
			}
		}
	}
}
</script>

<style>

</style>
